package com.tim.helper.project;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

public class JuShuiTanHelper {
    @Test
    public void list2Req() throws NoSuchFieldException {
    String str =
        "参数名称\t参数类型\t是否允许为空\t示例值\t参数描述\n"
            + "object\t\n"
            + "是\n"
            + "modified_begin\tstring\t\n"
            + "是\n"
            + "2021-12-01 18:34:13\t修改起始时间，和结束时间必须同时存在，时间间隔不能超过七天，与款式编码不能同时为空\n"
            + "modified_end\tstring\t\n"
            + "是\n"
            + "2021-12-07 18:34:13\t修改结束时间，和起始时间必须同时存在，时间间隔不能超过七天，与款式编码不能同时为空\n"
            + "only_item\tboolean\t\n"
            + "是\n"
            + "false\t是否只查询款式信息不返回商品明细";

        // 名称，必填，示例，类型，变量名
        String template = "/**\n*%5$s\t允许为空：%3$s\n*%4$s\n*/\nprivate %2$s %1$s;\n\n";

        str = str.trim()
                .replaceAll("\tstring\t", "\tString\t")
                .replaceAll("\tinteger\t", "\tInteger\t");

        String[] split = str.split("\n");
        for(int i=3; i<split.length; i=i+3){
            String e = split[i]+split[i+1]+"\t"+split[i+2];
            String[] strs = e.split("\t+");
            if (strs.length < 5) {
                System.out.println(strs[0]);
            }else{
                System.out.println(String.format(template, strs));
            }
        }

    }

    @Test
    public void jsonToReq() throws IOException {
        int isReq = 1;
        String jsonStr =
                Files.readAllLines(Paths.get("L:\\github\\Autil\\src\\main\\resources\\info.txt")).stream()
                        .collect(Collectors.joining());
        JSONObject json = JSON.parseObject(jsonStr);
        String reqJsonStr = json.getJSONObject("data").getString("reqJsonSchema");
        String respJsonStr = json.getJSONObject("data").getString("respJsonSchema");
        if(isReq==1){
            json = JSON.parseObject(reqJsonStr);
            JSONObject propertiesObj;
            if(json.getString("type").equals("array")){
                json = json.getJSONObject("items");
                System.out.println("// "+json.getString("description"));
                propertiesObj = json.getJSONObject("properties");
            }else{
                propertiesObj = json.getJSONObject("properties");
            }
            handleProperties(propertiesObj);
        }else{
            json = JSON.parseObject(respJsonStr);
            JSONObject propertiesObj = json.getJSONObject("properties")
                    .getJSONObject("data")
                    .getJSONObject("properties");
            if(propertiesObj.containsKey("orders")){
                propertiesObj = propertiesObj.getJSONObject("orders")
                        .getJSONObject("items")
                        .getJSONObject("properties");
            }
            handleProperties(propertiesObj);
        }


    }

    private void handleProperties(JSONObject propertiesObj) {
    System.out.println("{");
        for(String key : propertiesObj.keySet()){
            JSONObject propertyObj = propertiesObj.getJSONObject(key);
            if(propertyObj.getString("type").equals("object")){
                handleProperties(propertyObj.getJSONObject("properties"));
                System.out.println();
            }else{
                if(propertyObj.getString("type").equals("array")){
                    String type = propertyObj.getJSONObject("items").getString("type");
                    if(type.equals("object")){
                        // list
                        handleProperties(propertyObj.getJSONObject("items").getJSONObject("properties"));
                    }
                    propertyObj.put("type", "List<"+type.substring(0,1).toUpperCase()+type.substring(1)+">");
                }
            }
            // 名称，必填，示例，类型，变量名
            String template = "/**\n*%1$s\t%2$s\n*%3$s\n*/\nprivate %4$s %5$s;\n\n";
            String type = propertyObj.getString("type").substring(0,1).toUpperCase()+propertyObj.getString("type").substring(1);
            String mock = propertyObj.containsKey("mock") ? propertyObj.getJSONObject("mock").getString("mock"): "";
            System.out.println(String.format(template, new String[]{
                    propertyObj.getString("description"),
                    "",mock,type,key
            }));
        }
        System.out.println("}");
    }

}
